package com.sz.springboot_jpa.cntroller;

import com.sz.springboot_jpa.entity.Article;
import com.sz.springboot_jpa.entity.Category;
import com.sz.springboot_jpa.pojo.ResponseCode;
import com.sz.springboot_jpa.service.ArticleServer;
import com.sz.springboot_jpa.service.CategoryService;
import com.sz.springboot_jpa.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("article")
public class ArticleController {
    @Resource
    private CategoryService categoryService;
    @Resource
    private ArticleServer articleServer;



    /***
     * 发表博客文章或更新博客文章
     * @param article
     * @param session
     * @return
     */
    @ResponseBody
    @PostMapping
    public ResponseCode post(@RequestBody Article article,HttpSession session){
        //获取博客文章 id
        System.out.println("sdasdasdasdasd");
        Object writeId = session.getAttribute("writeId");
        ResponseCode code = new ResponseCode();
        //判断博客文章 id 是否为 null
        if(writeId == null){
            //如果为null，则表示
            //第一次发表
            code.setMsg("发表博客文章成功");
            code.setStatus(ResponseCode.SUCCESS);
            articleServer.post(article);
            session.setAttribute("writeId",article.getId());
        }else{
            //否则为更新
            article.setId(Long.parseLong(writeId.toString()));
            articleServer.update(article);
            code.setStatus(ResponseCode.SUCCESS);
            code.setMsg("更新博客文章成功！");

        }
        return code;
    }

    /***
     * 开始写文章
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("write")
    public String write(HttpSession session, Model model){
        //为了防止使用上一个的 博客文章 id
        //所以 delete 上一个的 博客文章 id
        session.removeAttribute("writeId");
        //获取 文章类别
        List<Category> categories = categoryService.findByParent(null);
        model.addAttribute("categories",categories);
        return "article/write";
    }

    /***
     * 根据 父id来查找 子分类
     * @param
     * @return
     */
    @ResponseBody
    @PostMapping("sub")
    public List<Category> subCategory( Integer parent){
        List<Category> categories2 = categoryService.findByParent(parent);
        return categories2;
    }

    /***
     * 博客阅读
     * @param id
     * @param model
     * @return
     */
    @GetMapping("{id}")
    public String findById(@PathVariable("id")Long id,Model model){

        //通过 文章id 获取你要获取文章
        Article article = articleServer.findById(id);
        model.addAttribute("article",article);
        return "article/article";
    }

    @RequestMapping("list")
    @ResponseBody
    public ResponseCode list(@RequestParam(value = "limit") Integer pageSize, @RequestParam(value = "page") Integer pageNum){
        ResponseCode code  = articleServer.find(pageSize,pageNum);
        return code;
    }

    /**
     * 展示文章细节
     */
    @RequestMapping("to/detail")
    public String toDetail(){
        return "article/detail";
    }

    @RequestMapping("detail")
    public Article detail(@RequestParam("id") Long id,Model model,HttpSession session){
        Optional<Article> article = articleServer.showDetailById(id);
        Article article1 = article.get();
//        String title = article.get().getTitle();
//        String content = article.get().getContent();
//        String summary = article.get().getSummary();
        model.addAttribute("article",article1);
        return article1;
    }
    /**
     * 修改文章
     */
    @RequestMapping("to/update")
    public String toUpdate(){
        return "article/update";
    }

    /**
     * 这个更新，jsp页面还没写完
     * @param model
     * @param session
     * @param article
     * @return
     */
    @RequestMapping("update")
    public Article update(Model model,HttpSession session,Article article){
        Article article1 =  articleServer.update(article);
        return article;
    }

    /**
     * 跳转到删除
     * @return
     */
    @RequestMapping("to/del")
    public String toDel(){
        return "article/delete";
    }

    /**
     * 通过id删除指定的文章
     * @param id
     * @return
     */
    @RequestMapping("del")
    public String del(@RequestParam("id") Long id){
        articleServer.delById(id);
        return "main";
    }

}
